package com.dingding.util;

import com.dingding.config.URLConstant;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentListRequest;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @description: 部门列表
 * @author: zyj
 * @create: 2020-03-10 10:31
 **/
@Service
public class DepartMentUtil {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AccessTokenUtil accessTokenUtil;

    /**
    * @Description: 根据父id获取子部门信息失败
    * @Param: [parentID]
    * @return: java.util.List<java.util.Map>
    * @Author: zyj
    * @Date: 2020/3/10
    */
    public List<OapiDepartmentListResponse.Department> getDeptListByParentId(String parentID){
        try {
            DingTalkClient client = new DefaultDingTalkClient(URLConstant.DEPT_LIST);
            OapiDepartmentListRequest req = new OapiDepartmentListRequest();
            req.setFetchChild(false);
            req.setId(parentID);
            req.setHttpMethod("GET");
            OapiDepartmentListResponse rsp = client.execute(req, accessTokenUtil.getToken(URLConstant.COMPANY));
            long errorCode = rsp.getErrcode();
            if(0==errorCode){
                List<OapiDepartmentListResponse.Department> departments = rsp.getDepartment();
                return departments;
            }
            logger.error("根据父id获取子部门信息失败，父id={},msg={}", parentID, rsp.getBody());
        }catch (Exception e){
            logger.error("根据父id获取子部门信息失败，父id={}", parentID, e);
        }
        return null;
    }

}
